﻿using System;
using UnityEngine;
[Serializable]
public class TestClass
{
    public TestClass(Transform transform)
    {
        this.transform = transform;
        this.boxCollider = transform.GetComponent<BoxCollider2D>();
        objectVisual = transform;
    }

    private BoxCollider2D boxCollider;
    private Transform transform;
    private Vector2 moveDelta;
    RaycastHit2D hit;
    [SerializeField] private LayerMask cantMoveLayerMask;//碰撞检测层
    [SerializeField] private Transform objectVisual;
    [SerializeField] float ySpeed = 0.75f;
    [SerializeField] float xSpeed = 1.0f;
    
    [SerializeField] float pushRecoverySpeed;
    protected Vector2 moveDirection;
    protected Vector2 pushDirection;
    
    
    
    public virtual void UpdateMotor(Vector2 input)
    {
        moveDelta = new Vector2(input.x * xSpeed, input.y * ySpeed);

        if (moveDelta.x > 0)
        {
            objectVisual.localScale = Vector3.one;
        }
        else if (moveDelta.x < 0)
        {
            objectVisual.localScale = new Vector3(-1, 1, 1);
        }

        //添加推力
        moveDelta += pushDirection;
        pushDirection = Vector3.Lerp(pushDirection, Vector3.zero, pushRecoverySpeed);

        hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, moveDelta.y),
            Mathf.Abs(moveDelta.y * Time.deltaTime), cantMoveLayerMask);
        if (hit.collider == null)
        {
            transform.Translate(0, moveDelta.y * Time.deltaTime, 0);
        }

        hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0),
            Mathf.Abs(moveDelta.x * Time.deltaTime), cantMoveLayerMask);
        if (hit.collider == null)
        {
            transform.Translate(moveDelta.x * Time.deltaTime, 0, 0);
        }
    }

    protected virtual void OnPushBack(Vector2 pushDirection,float pushBackForce)
    {
        this.pushDirection = pushDirection * pushBackForce;
    }
}